package cemp.service;

import cemp.dto.DailySubjectDto;
import cemp.dto.DailyTaskDto;
import cemp.entity.DailySubjectItem;
import cemp.entity.DailyTask;
import cemp.mapper.DailySubjectItemMapper;
import cemp.mapper.DailySubjectOutlineMapper;
import cemp.mapper.DailyTaskMapper;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;


@RequiredArgsConstructor
@Slf4j
@Service
public class DailySubjectService {

    @Resource
    private final DailySubjectOutlineMapper outlineMapper;

    @Resource
    private final DailySubjectItemMapper itemMapper;

    public List<DailySubjectDto> getAll(){
        List<DailySubjectItem> list =   itemMapper.selectList(null);
        Map<Integer,Integer> rootMap = list.stream().filter(new Predicate<DailySubjectItem>() {
            @Override
            public boolean test(DailySubjectItem item) {
                return item.getType() == 2;
            }
        }).collect(Collectors.toMap(DailySubjectItem::getId, DailySubjectItem::getParentId, (v1, v2) -> v2));

        Set<Integer> parentSet = list.stream().filter(new Predicate<DailySubjectItem>() {
            @Override
            public boolean test(DailySubjectItem item) {
                return item.getParentId() != null;
            }
        }).map(DailySubjectItem::getParentId).distinct().collect(Collectors.toSet());
        //
        List<DailySubjectDto> result =  list.stream().map(new Function<DailySubjectItem, DailySubjectDto>() {
            @Override
            public DailySubjectDto apply(DailySubjectItem item) {
                DailySubjectDto dto = new DailySubjectDto();
                BeanUtil.copyProperties(item,dto);
                if(dto.getType() == 1){
                    dto.setRootId(dto.getId());
                }else if (dto.getType() ==2){
                    dto.setRootId(dto.getParentId());
                }else if (dto.getType() ==3){
                    dto.setRootId(rootMap.get(dto.getParentId()));
                }
                if(dto.getParentId() == null){
                    dto.setParentId(0);
                }
                dto.setHasSons(parentSet.contains(dto.getId()));        //设置节点是否有叶子节点
                return dto;
            }
        }).collect(Collectors.toList());

        //排序 - 1 (算法有问题)
//        result = result.stream().sorted(new Comparator<DailySubjectDto>() {
//            @Override
//            public int compare(DailySubjectDto o1, DailySubjectDto o2) {
//
//                if(o1.getRootId().intValue() == o2.getRootId().intValue()){
//                    if(o1.getParentId().equals(o2.getParentId())){
//                        return o1.getId().compareTo(o2.getId());
//                    }else{
//                        return o1.getParentId().compareTo(o2.getParentId());
//                    }
//                }else{
//                    return o1.getRootId().compareTo(o2.getRootId());
//                }
//            }
//        }).collect(Collectors.toList());

        //算法2
        List<DailySubjectDto> list1 = result.stream().filter(dto -> dto.getType() <3).collect(Collectors.toList());
        List<DailySubjectDto> list2 = result.stream().filter(dto -> dto.getType() == 3).collect(Collectors.toList());
        //Map<Integer,List<DailySubjectDto>> map1 = list1.stream().collect(Collectors.groupingBy(DailySubjectDto::getParentId));
        list1 = list1.stream().sorted((o1, o2) -> {
            if(o1.getRootId().equals(o2.getRootId())){
                return o1.getParentId().compareTo(o2.getParentId());
            }else{
                return o1.getRootId().compareTo(o2.getRootId());
            }
        }).collect(Collectors.toList());
        Map<Integer,List<DailySubjectDto>> map2 = list2.stream().collect(Collectors.groupingBy(DailySubjectDto::getParentId));
        for (int i = list1.size()-1; i >0 ; i--) {
            if(map2.containsKey(list1.get(i).getId())){
                list1.addAll(i+1,  map2.get(list1.get(i).getId()));
            }
        }
        //算法3 用分值计算
        return list1;
    }


    public static void main(String[] args) {
//        Integer [] a1 = new Integer[]{1,2,3,4};
//        Integer [] a2 = new Integer[]{6,7};
//        List<Integer> l1 = Arrays.asList(a1);
//        List<Integer> l2 = Arrays.asList(a2);

        List<Integer> l1 = new ArrayList<>();
        List<Integer> l2 = new ArrayList<>();
        l1.add(1);
        l1.add(2);
        l1.add(3);
        l1.add(4);
        l2.add(5);
        l2.add(6);
        l1.addAll(2,l2);
        System.out.println("123");
    }

    public void doadd(DailySubjectDto subjet) {
        DailySubjectItem item = new DailySubjectItem();
        BeanUtil.copyProperties(subjet,item);
        itemMapper.insert(item);
    }

    /**
     * 删除主题和主题下所有的子主题
     * @param subject
     */
    public void doDelete(DailySubjectDto subject) {
        List<Integer> list = new ArrayList<>();
        //查询所有关联主题
        if(1==subject.getType()){
            LambdaQueryWrapper<DailySubjectItem> lqw = new LambdaQueryWrapper<>();
            lqw.eq(DailySubjectItem::getParentId,subject.getId());
            List<Integer> ids = itemMapper.selectList(lqw).stream().map(DailySubjectItem::getId).collect(Collectors.toList());
            list.addAll(ids);
            lqw.clear();
            lqw.in(DailySubjectItem::getParentId,ids);
            list.addAll(itemMapper.selectList(lqw).stream().map(DailySubjectItem::getId).collect(Collectors.toList()));
        }else if (2 == subject.getType()){
            LambdaQueryWrapper<DailySubjectItem> lqw = new LambdaQueryWrapper<>();
            lqw.eq(DailySubjectItem::getParentId,subject.getId());
            list.addAll(itemMapper.selectList(lqw).stream().map(DailySubjectItem::getId).collect(Collectors.toList()));
        }else if (3==subject.getType()) {

        }
        list.add(subject.getId());
        itemMapper.deleteBatchIds(list);
    }
}
